package cn.iocoder.yudao.module.member.dal.mysql.order;

import cn.hutool.core.date.DateTime;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.controller.admin.order.vo.OrderUserCounts;
import cn.iocoder.yudao.module.member.controller.admin.order.vo.OrderUserCountsByTime;
import cn.iocoder.yudao.module.member.controller.app.order.vo.AppMemberCertificateOrderRecordPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.order.MemberCertificateOrderDo;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
@InterceptorIgnore(tenantLine = "true")
public interface MemberCertificateOrderMapper extends BaseMapperX<MemberCertificateOrderDo> {
     MemberCertificateOrderDo selectByUdidAndPurchaseType(@Param("udid") String udid,@Param("classType") String classType, @Param("userId") long userId);




    default PageResult<MemberCertificateOrderDo> selectPage(Long loginUserId, AppMemberCertificateOrderRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MemberCertificateOrderDo>()
                .eqIfPresent(MemberCertificateOrderDo::getUdid, reqVO.getUdid())
                .eqIfPresent(MemberCertificateOrderDo::getCerStatus,reqVO.getCerStatus())
                .likeIfPresent(MemberCertificateOrderDo::getRemark, reqVO.getRemark())
                .eqIfPresent(MemberCertificateOrderDo::getUserId, loginUserId)
                .eq(ObjectUtils.isNotNull(reqVO.getDropFlag())  ,MemberCertificateOrderDo::getDropFlag, reqVO.getDropFlag())
                .orderByDesc(MemberCertificateOrderDo::getId));
    }

    /**
     * 根据时间查询数据
     * @param beginOfDay
     * @param endOfDay
     * @return
     */
    List<OrderUserCountsByTime> selectCountBetween(@Param("beginOfDay") DateTime beginOfDay, @Param("endOfDay")DateTime endOfDay);


    default  Long selectCountByYearAndMonth(int year,int month,String purchaseType){
        return selectCount(new LambdaQueryWrapperX<MemberCertificateOrderDo>().eq(MemberCertificateOrderDo::getYear,year).eq(MemberCertificateOrderDo::getMonth,month).eq(MemberCertificateOrderDo::getClassType,purchaseType));
    }

    List<Long> selectCountByUser(@Param("yesterdayBegin")  String yesterdayBegin,@Param("yesterdayEnd") String yesterdayEnd);

    List<OrderUserCounts>  selectCountByUserId(@Param("userIds") List<Long> userIds, @Param("type")int type,@Param("yesterdayBegin") String yesterdayBegin,@Param("yesterdayEnd") String yesterdayEnd);

    default MemberCertificateOrderDo selectByUdidAndUserId(String udid, Long userId) {
        return selectOne(new LambdaQueryWrapperX<MemberCertificateOrderDo>()
                .eq(MemberCertificateOrderDo::getUdid, udid)
                .eq(MemberCertificateOrderDo::getUserId, userId)
                .eq(MemberCertificateOrderDo::getDropFlag,false)
                .orderByDesc(BaseDO::getUpdateTime)
                .last("LIMIT 1")
        );
    }

    /**
     * 查询用户+udid 未掉签且存在描述文件的设备证书
     * @param userId
     * @param udid
     * @return
     */
    default List<MemberCertificateOrderDo> getListByUserAndUdid( Long userId,String udid){
       return selectList(new LambdaQueryWrapperX<MemberCertificateOrderDo>()
               .eq(MemberCertificateOrderDo::getUdid,udid)
               .eq(MemberCertificateOrderDo::getUserId,userId)
               .eq(MemberCertificateOrderDo::getDropFlag,0 ).isNotNull(MemberCertificateOrderDo::getMpUrl));
    }

    List<MemberCertificateOrderDo> selectOtherSysFileList();


    void updateMpUrl(@Param("id") long id,@Param("mpURl") String mpURl,@Param("cerName") String cerName);
}
